import { createSlice } from "@reduxjs/toolkit"

const initialState = {
    name: "张三",
    age: 0,
}

const test1Store = createSlice({
    // 模块名
    name: "test1",

    // 初始状态
    initialState: initialState,

    // 用于状态修改的同步方法
    reducers: {
        setName(state, name) {
            state.name = name
        },
        setAge(state) {
            state.age = state.age + 1
        },
        // 异步方法
        asyncSetAge(state) {
            setTimeout(() => {
                state.age = state.age + 1
            }, 1000)
        },
        // 重置数据
        resetData(state) {
            return {
                ...initialState,
            }
        },
    },
})

const { setName, setAge, resetData } = test1Store.actions // 解构出同步方法

const test1Reducer = test1Store.reducer // 获取reducer

export { setName, setAge, resetData, test1Reducer }
